<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
    session_start();
/*    
    try {
        $dbh = new PDO("mysql:host=localhost;dbname=cloudcom", "zhouzhao", "19831022");
  
        //begin a trasaction and turn off auto-commit
        $dbh->beginTransaction();
        
        $sql = "insert into orders values(null, ".$_SESSION["customerID"].", '".date("Y-m-d")."', ".$_SESSION["totalPrice"].", '".$_SESSION["cardNum"]."', '".$_SESSION["cardName"]."', '".$_SESSION["expDate"]."')";
        
//        echo $sql;
        $dbh->exec($sql);                
        $orderID = $dbh->lastInsertId();
        
        foreach ($dbh->query("select * from shoppingCarts S, products P where S.productID=P.productID and S.sessionID=".$_SESSION["customerID"]) as $row) {
            $sql = "insert into items values(null, $orderID, ".$row["productID"].", ".$row["productQuantity"].", ".$row["productPrice"].")";
//            echo $sql;
            $dbh->exec($sql);
        }
        
        $dbh->exec("delete from shoppingCarts where sessionID=".$_SESSION["customerID"]);
        
        //commit the changes
        $dbh->commit();
        $dbh = NULL;
        
        require 'showOrder.php';

} catch (PDOException $exc) {
    echo "error: ".$exc->getMessage()."<br/>";
    die();
}
*/    
    $con = mysql_connect("localhost", "zhouzhao", "19831022");
    if(!$con){
        die("could not connect to DB: ".mysql_error());
    }
    mysql_select_db("cloudcom", $con);
    
    mysql_query("insert into orders values(null, ".$_SESSION["customerID"].", '".date("Y-m-d")."', ".$_SESSION["totalPrice"].", '".$_SESSION["cardNum"]."', '".$_SESSION["cardName"]."', '".$_SESSION["expDate"]."')");
    
    $orderID = mysql_fetch_assoc(mysql_query("select orderID from orders where customerID=".$_SESSION["customerID"]." and cardNum='".$_SESSION["cardNum"]."'"));
    
    $result = mysql_query("select * from shoppingCarts S, products P where S.productID=P.productID and S.sessionID=".$_SESSION["customerID"]);
    
    while ($row = mysql_fetch_assoc($result)) {
        mysql_query("insert into items values(null, ".$orderID["orderID"].", ".$row["productID"].", ".$row["productQuantity"].", ".$row["productPrice"].")");
    }
    
    mysql_query("delete from shoppingCarts where sessionID=".$_SESSION["customerID"]);
    
    require "showOrder.php";

   
?>
